#include "controlDevice.h"
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include "voice.h"
#include "dht11.h"


#define HIGH_TIME 32

typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;

uint32 databuf;



/*

struct controlDevice
{
    char name[128]; // 设备名称
    int pin;  // 设别引脚
    int pinStatus; // 引脚状态
    int pinMode; // 引脚模式
    int (*deviceInit)(); // 初始化函数
    int (*open)(); // 打开函数
    int (*close)(); // 关闭函数
    int (*clearConfig)(); // 清除内存

    struct controlDevice *Next;
};


 */


int dht11Clear(void);

int dht11Init(void);

int dht11_open(void);


struct controlDevice dht11 = {
    .name       = "dht11",
    .pin        = 0,
    .pinStatus  = HIGH,
    .pinMode    = OUTPUT,
    .deep_flag  = 1,
    .deviceInit = dht11Init,
    .clearConfig= dht11Clear,
    .get        = dht11Get,
    .open       = dht11_open
};


int dht11_open(void)
{
    uint8 crc;
    uint8 i;

    pinMode(dht11.pin, OUTPUT); // set mode to output
    digitalWrite(dht11.pin, 0); // output a high level
    delay(25);
    digitalWrite(dht11.pin, 1); // output a low level
    pinMode(dht11.pin, INPUT);  // set mode to input
    pullUpDnControl(dht11.pin, PUD_UP);

    delayMicroseconds(27);
    if (digitalRead(dht11.pin) == 0) // SENSOR ANS
    {
        while (!digitalRead(dht11.pin)); // wait to high

        for (i = 0; i < 32; i++)
        {
            while (digitalRead(dht11.pin)); // data clock start
            while (!digitalRead(dht11.pin)); // data start
            delayMicroseconds(HIGH_TIME);
            databuf *= 2;
            if (digitalRead(dht11.pin) == 1) // 1
            {
                databuf++;
            }
        }

        for (i = 0; i < 8; i++)
        {
            while (digitalRead(dht11.pin)); // data clock start
            while (!digitalRead(dht11.pin)); // data start
            delayMicroseconds(HIGH_TIME);
            crc *= 2;
            if (digitalRead(dht11.pin) == 1) // 1
            {
                crc++;
            }
        }
        return 1;
    }
    else
    {
        return 0;
    }
}


int dht11Clear(void)
{
    
}


int dht11Init(void)
{  
    pinMode(dht11.pin,dht11.pinMode); // set mode to output
    digitalWrite(dht11.pin,dht11.pinStatus);   // output a high level

    printf("Enter OS-------\n");
}

void* dht11Get(void * arg)
{
    pthread_detach(pthread_self());
    int fd = -1;
    unsigned char buffer[8] = {0x09,0x08,0x0A,0x00,0x08,0x09};
    if(arg != NULL)
    {
        fd = *((int* )arg);
    }
    else
    {
        pthread_exit(0);
    }
    while (1)
    {
        
        pinMode(dht11.pin, OUTPUT); // set mode to output
        digitalWrite(dht11.pin, 1); // output a high level
        delay(3000);
        if (dht11_open())
        {
            printf("Congratulations ! Sensor data read ok!\n");
            printf("RH:%d.%d\n", (databuf >> 24) & 0xff, (databuf >> 16) & 0xff);
            buffer[3] = (databuf >> 24) & 0xff;
            serialSendstring(fd,buffer,6);
            printf("TMP:%d.%d\n", (databuf >> 8) & 0xff, databuf & 0xff);
            buffer[2] = 0x0B;
            buffer[3] = (databuf >> 8) & 0xff;
            buffer[4] =  databuf & 0xff;
            buffer[5] = 0x08;
            buffer[6] = 0x09;
            serialSendstring(fd,buffer,7);
            databuf = 0;
            unsigned char buf[6] = {0x09,0x08,0x37,0x00,0x08,0x09};// 09 08 37 00 08 09
            serialSendstring(fd,buf,6);
            pthread_exit(0);
        }
        else
        {
            printf("Sorry! Sensor dosent ans!\n");
            databuf = 0;
        }
        
    }
}

struct controlDevice* adddht11ToDeviceLink(struct controlDevice* phead)
{
    if(phead == NULL)
    {
        return &dht11;
    }
    else
    {
        dht11.Next = phead;
        phead = &dht11;
        return phead;
    }

}

